package cn.silver.module.configuration.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.silver.framework.log.annotation.Log;
import cn.silver.framework.log.enums.OperateEnums;
import cn.silver.framework.mybatis.controller.DataController;
import cn.silver.framework.openapi.model.Result;
import cn.silver.module.configuration.domain.SysParam;
import cn.silver.module.configuration.bean.ParamBean;
import cn.silver.module.configuration.param.ConfigParam;
import cn.silver.module.configuration.service.ISysParamService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 参数配置 信息操作处理
 *
 * @author Silver Zhou
 */
@Validated
@RestController
@RequiredArgsConstructor
@Tag(name = "配置管理-参数配置")
@RequestMapping("/config/param")
public class SysParamController extends DataController<ISysParamService, SysParam, ConfigParam, ParamBean> {

    @Resource
    public void setBaseService(ISysParamService baseService) {
        this.setCode("config:param");
        this.setTitle("配置管理-参数配置");
        this.baseService = baseService;
    }

    /**
     * 根据参数键名查询参数值
     *
     * @param configKey 参数Key
     */
    @Operation(summary = "根据参数键名查询参数值")
    @GetMapping(value = "/configKey/{configKey}")
    public Result<Void> getConfigKey(@PathVariable String configKey) {
        return Result.ok(baseService.getConfigValue(configKey));
    }

    /**
     * 刷新参数缓存
     */
    @DeleteMapping("/refreshCache")
    @Operation(summary = "刷新参数缓存")
    @SaCheckPermission("system:config:remove")
    @Log(title = "参数管理", operate = OperateEnums.ALTER_CLEAR)
    public Result<Void> refreshCache() {
        baseService.resetConfigCache();
        return Result.ok();
    }
}
